﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EasyCommonCore.Common
{
    /// <summary>
    /// 列表类型帮助类
    /// </summary>
    public static class ListTypeCommon
    {
        #region List与String互转

        #region 字符串转换为泛型集合
        /// <summary>
        /// 字符串转化为泛型集合
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="splitstr">要分割的字符,默认以,分割</param>
        /// <returns></returns>
        public static List<T> ConvertStrToList<T>(this string str, char splitstr = ',')
        {
            if (string.IsNullOrEmpty(str))
            {
                return new List<T>();
            }
            string[] strarray = str.Split(splitstr);
            return (from s in strarray where s != "" select (T)Convert.ChangeType(s, typeof(T))).ToList();
        }
        #endregion

        #region List转换为字符串
        /// <summary>
        /// List转换为字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterList">list类型</param>
        /// <param name="splitstr">要分割的字符,默认以,分割</param>
        /// <returns></returns>
        public static string CovertListToStr<T>(this List<T> parameterList, char splitstr = ',')
        {
            string result = parameterList.Aggregate("", (current, i) => current + i.ToString() + splitstr);
            if (!string.IsNullOrWhiteSpace(result))
            {
                result = result.Substring(0, result.Length - 1);
            }
            return result;
        }
        #endregion

        #endregion

        #region List类型相加减

        #region 合并两个类型一致的泛型集合
        /// <summary>
        /// 合并两个类型一致的泛型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="a">泛型A</param>
        /// <param name="b">泛型B</param>
        /// <returns></returns>
        public static List<T> AddRanges<T>(this List<T> a, List<T> b)
        {
            if (a == null)
            {
                a = new List<T>();
            }
            if (b == null)
            {
                b = new List<T>();
            }
            List<T> c = a.ToList();
            c.AddRange(b);
            return c;
        }

        #endregion

        #region 得到参数1中不包含参数2的集合
        /// <summary>
        /// 得到参数1中不包含参数2的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static List<T> Minus<T>(this List<T> a, List<T> b)
        {
            if (a == null)
            {
                a = new List<T>();
            }
            if (b == null)
            {
                b = new List<T>();
            }
            List<T> temp = a.DeepClone();
            foreach (var item in b)
            {
                if (temp.Contains(item))
                    temp.Remove(item);
            }
            return temp;
        } 
        #endregion

        #endregion

        #region 得到新增的集合以及被删除的集合
        /// <summary>
        /// 得到新增的集合以及被删除的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="oldObject">老的集合</param>
        /// <param name="newOject">新的集合</param>
        /// <param name="newAddObject">新增加的集合</param>
        /// <param name="deleteObject">被删除的集合</param>
        /// <param name="alwaysExist">总是存在的集合</param>
        public static void ConvertToCheckData<T>(List<T> oldObject, List<T> newOject, out List<T> newAddObject,
            out List<T> deleteObject, out List<T> alwaysExist)
        {
            newAddObject = new List<T>();
            deleteObject = new List<T>();
            alwaysExist = new List<T>();
            if (oldObject == null)
            {
                oldObject = new List<T>();
            }
            if (newOject == null)
            {
                newOject = new List<T>();
            }
            foreach (var objects in oldObject)
            {
                if (newOject.Contains(objects))
                {
                    newAddObject.Remove(objects);//从新的中删除原来已存在的
                    alwaysExist.Add(objects);//始终存在的
                }
                else
                {
                    deleteObject.Add(objects);//被删除的
                }
            }
        }

        #endregion

    }
}
